package com.zprmb.udzt;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.Regular;
import com.zprmb.udzt.util.VolleryUtil;

public class Update_login_pw extends BaseActivity implements OnClickListener{
	private ImageView img_back,pass,pass2,pass3;
	private EditText old_pwd,new_pwd,new_confirm;
	private Button btn_confirm;
	private RelativeLayout rl_back;
	private TextView tv_customer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.set_login_pw);
		 BbtApplication.getInstance().addActvity(this);
		img_back=(ImageView)findViewById(R.id.img_back);
		old_pwd=(EditText)findViewById(R.id.old_pwd);
		new_pwd=(EditText)findViewById(R.id.new_pwd);
		new_confirm=(EditText)findViewById(R.id.new_confirm);
		btn_confirm=(Button)findViewById(R.id.btn_confirm);
		pass=(ImageView)findViewById(R.id.img_pass);
		pass2=(ImageView)findViewById(R.id.img_pass2);
		pass3=(ImageView)findViewById(R.id.img_pass3);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		tv_customer=(TextView)findViewById(R.id.enterprise_tel);
		tv_customer.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		pass.setOnClickListener(this);
		pass2.setOnClickListener(this);
		pass3.setOnClickListener(this);
		img_back.setOnClickListener(this);
		btn_confirm.setOnClickListener(this);
		old_pwd.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(old_pwd.getText().toString().equals("")){
					pass.setVisibility(View.GONE);
				}else{
					pass.setVisibility(View.VISIBLE);
				}	
			}
		});
		new_pwd.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(new_pwd.getText().toString().equals("")){
					pass2.setVisibility(View.GONE);
				}else{
					pass2.setVisibility(View.VISIBLE);
				}
				
			}
		});
		new_confirm.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(new_confirm.getText().toString().equals("")){
					pass3.setVisibility(View.GONE);
				}else{
					pass3.setVisibility(View.VISIBLE);
				}
			}
		});
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	private void initData() {
		// TODO Auto-generated method stub
		AjaxParams params=new AjaxParams();
		params.put("currentPassword", old_pwd.getText().toString());
		params.put("newPassword", new_pwd.getText().toString());
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if(jsonObject.optString("success").equals("1")){
					Toast.makeText(Update_login_pw.this, "修改成功", 2000).show();
					Intent intent=new Intent(Update_login_pw.this,Updata_Success.class);
					intent.putExtra("title", "修改登录密码");
					intent.putExtra("status", "修改成功");
					startActivity(intent);
					Update_login_pw.this.finish();
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(1);
		volleryUtil.execute(Update_login_pw.this, Constant.URL+"/apiAccount/sendChangeLoginPass", "?"+params.toString());
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.img_back:
			finish();
			break;
		case R.id.btn_confirm:
			if(old_pwd.getText().toString().equals(new_pwd.getText().toString())){
				Toast.makeText(Update_login_pw.this, "新密码不能与原密码一致", 2000).show();
				return;
			}
			if(!new_pwd.getText().toString().equals(new_confirm.getText().toString())){
				Toast.makeText(Update_login_pw.this, "两次输入密码不一致", 2000).show();
				return;
			}if(old_pwd.getText().toString().equals("")){
				Toast.makeText(Update_login_pw.this, "原密码不能为空", 2000).show();
				return;
			}
			if(new_pwd.getText().toString().equals("")){
				Toast.makeText(Update_login_pw.this, "请输入新密码", 2000).show();
				return;
			}
			if(new_confirm.getText().toString().equals("")){
				Toast.makeText(Update_login_pw.this, "请再次确认修改密码", 2000).show();
				return;
			}
			if(!Regular.getPass(new_confirm.getText().toString().trim())){
				Toast.makeText(Update_login_pw.this, "“登录密码可以为6-16位字符，可使用字母且分区大小写、数字、下划线且不包含空格”", 2000).show();
				return;
			}
			initData();
			break;
		case R.id.img_pass:
			if(old_pwd.getTransformationMethod().equals(PasswordTransformationMethod  
                    .getInstance())){
				old_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				pass.setBackgroundResource(R.drawable.yan1);
			}else{
				old_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
				pass.setBackgroundResource(R.drawable.yan2);
			}
			break;
		case R.id.img_pass2:
			if(new_pwd.getTransformationMethod().equals(PasswordTransformationMethod  
                    .getInstance())){
				new_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				pass2.setBackgroundResource(R.drawable.yan1);
			}else{
				new_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
				pass2.setBackgroundResource(R.drawable.yan2);
			}
			break;
		case R.id.img_pass3:
			if(new_confirm.getTransformationMethod().equals(PasswordTransformationMethod  
                    .getInstance())){
				new_confirm.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				pass3.setBackgroundResource(R.drawable.yan1);
			}else{
				new_confirm.setTransformationMethod(PasswordTransformationMethod.getInstance());
				pass3.setBackgroundResource(R.drawable.yan2);
			}
			break;
		case R.id.rl_back:
			finish();
			break;

		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		}
	}

	

}
